import { Exclude } from 'class-transformer';
import {
  BeforeInsert,
  Column,
  Entity,
  OneToMany,
  ManyToOne,
  CreateDateColumn,
  UpdateDateColumn,
  PrimaryGeneratedColumn,
} from 'typeorm';
import * as bcrypt from 'bcryptjs';
import { ApiProperty } from '@nestjs/swagger';
import { PostsEntity } from 'src/modules/posts/posts.entity';

@Entity('user')
export class User {
  /**
   * 检测密码是否一致
   * @param password0 加密前密码
   * @param password1 加密后密码
   */
  static comparePassword(password0, password1) {
    return bcrypt.compareSync(password0, password1);
  }

  static encryptPassword(password) {
    return bcrypt.hashSync(password, 10);
  }

  @ApiProperty({ description: '用户id' })
  @PrimaryGeneratedColumn('uuid')
  id: number;

  @ManyToOne(() => User, (user) => user.thirdAccount)
  user: User;

  @ApiProperty({ description: '用户名' })
  @Column({ length: 500, nullable: true })
  username: string; // 用户名

  @ApiProperty({ description: '用户昵称' })
  @Column({ length: 500, nullable: true })
  nickname: string; //昵称

  @ApiProperty()
  @Exclude()
  @Column({ select: false, nullable: true })
  password: string; // 密码

  @ApiProperty({ description: '头像' })
  @Column({ length: 500, default: null })
  avatar: string; //头像

  @ApiProperty({ description: '邮箱' })
  @Column({ length: 500, default: null, unique: true })
  email: string;

  @ApiProperty({ description: '邮箱认证' })
  @Column({ default: false, name: 'emial_verified' })
  emailVerified: boolean; // 邮箱

  @ApiProperty({ description: '描述' })
  @Column({ length: 500, default: null })
  description: string;

  @ApiProperty({ description: '积分' })
  @Column({ default: 0 })
  score: number;

  @ApiProperty({ description: '话题数量' })
  @Column({ name: 'topic_count', default: 0 })
  topicCount: number;

  @ApiProperty({ description: '评论数量' })
  @Column({ name: 'comment_count', default: 0 })
  commentCount: number;

  @ApiProperty({ description: '粉丝数量' })
  @Column({ name: 'fans_count', default: 0 })
  fansCount: number;

  @ApiProperty({ description: '关注数量' })
  @Column({ name: 'follow_count', default: 0 })
  followCount: number;

  @ApiProperty({ description: '角色' })
  @Column('enum', { enum: ['root', 'author', 'visitor'], default: 'visitor' })
  role: string; // 用户角色

  @ApiProperty({ description: '用户状态' })
  @Column('simple-enum', { enum: ['locked', 'active'], default: 'active' })
  status: string; // 用户状态

  @ApiProperty({ description: '禁言时间' })
  @Column({
    type: 'datetime',
    comment: '禁言时间',
    name: 'forbidden_end_time',
    nullable: true,
  })
  forbiddenEndTime: Date;

  @ApiProperty()
  @OneToMany(() => ThirdAccount, (thirdAccount) => thirdAccount.user)
  thirdAccount: ThirdAccount[];

  @OneToMany(() => PostsEntity, (post) => post.author)
  posts: PostsEntity[];

  // @Column({
  //   name: 'create_time',
  //   type: 'timestamp',
  //   default: () => 'CURRENT_TIMESTAMP',
  // })
  // createTime: Date;

  // @Exclude()
  // @Column({
  //   name: 'update_time',
  //   type: 'timestamp',
  //   default: () => 'CURRENT_TIMESTAMP',
  // })
  // updateTime: Date;

  @CreateDateColumn({
    type: 'datetime',
    comment: '创建时间',
    name: 'create_at',
  })
  createAt: Date;

  @UpdateDateColumn({
    type: 'datetime',
    comment: '更新时间',
    name: 'update_at',
  })
  updateAt: Date;

  /**
   * 插入数据前，对密码进行加密
   */
  @BeforeInsert()
  async encryptPwd() {
    if (!this.password) return;
    this.password = await bcrypt.hashSync(this.password, 10);
  }
}
